﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Xml;

namespace UFSControl
{
    public partial class DeviceInfo : Form
    {
        private string vBaseURL;

        public DeviceInfo()
        {
            InitializeComponent();
            IniFile lIni = new IniFile();
            var lip = lIni.Read("IP", "Receiver");
            vBaseURL = "http://" + lip + ":9000/xmlfeeds/deviceInfo";
            ReadDeviceInfo();
        }

        private void ReadDeviceInfo()
        {
            Uri lUri = new Uri(vBaseURL);
            string lResponse = SendWebRequest(lUri);

            //wenn keine Daten gefunden, dann exit
            if (lResponse == "") { return; }
            //Bug im XML, hier muss ein Tag korrigiert werden
            lResponse = lResponse.Replace("deviceInfoInfoTable", "deviceInfoTable");
            //XML Elemente durchgehen
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(lResponse);
            XmlElement root = doc.DocumentElement;
            XmlNodeList nodes = root.SelectNodes("./deviceInfo");
            try
            {

                foreach (XmlNode i in nodes)
                {
                    if (i["xmlInterfaceVersion"] != null)
                    {
                        listView1.Items.Add(new ListViewItem(new string[] { "Interface Version", i["xmlInterfaceVersion"].InnerText }));
                    }
                    if (i["ipAdress"] != null)
                    {
                        listView1.Items.Add(new ListViewItem(new string[] { "IP Adresse", i["ipAdress"].InnerText }));
                    }
                    if (i["productType"] != null)
                    {
                        listView1.Items.Add(new ListViewItem(new string[] { "Gerät", i["productType"].InnerText }));
                    }
                    if (i["VirtualStandBy"] != null)
                    {
                        listView1.Items.Add(new ListViewItem(new string[] { "Virtual Standby", i["VirtualStandBy"].InnerText }));
                    }
                    if (i["textInput"] != null)
                    {
                        listView1.Items.Add(new ListViewItem(new string[] { "Text Eingabe", i["textInput"].InnerText }));
                    }
                    if (i["TVGenial"] != null)
                    {
                        listView1.Items.Add(new ListViewItem(new string[] { "TV Genial", i["TVGenial"].InnerText }));
                    }
                    if (i["liveChannel"] != null)
                    {
                        listView1.Items.Add(new ListViewItem(new string[] { "aktiver Kanal", i["liveChannel"].InnerText }));
                    }
                    if (i["redBull"] != null)
                    {
                        listView1.Items.Add(new ListViewItem(new string[] { "Red Bull Funktionen", i["redBull"].InnerText }));
                    }
                    if (i["wol"] != null)
                    {
                        listView1.Items.Add(new ListViewItem(new string[] { "Wake on Lan", i["wol"].InnerText }));
                    }
                    if (i["mac"] != null)
                    {
                        listView1.Items.Add(new ListViewItem(new string[] { "Mac Adresse", i["mac"].InnerText }));
                    }
                }

            }
            catch
            {
            }
        }

        //an eine WEbadresse einen REquest schicken und das Ergebnis als String zurückliefern
        private string SendWebRequest(Uri aURL)
        {
            string lresponseFromServer = "";

            WebRequest lRequest = WebRequest.Create(aURL);
            ((HttpWebRequest)lRequest).UserAgent = "Eigenbau";
            lRequest.Method = "GET";
            lRequest.ContentType = "text/html";
            //Stream ldataStream = lrequest.GetRequestStream();
            // Set the 'Timeout' property in Milliseconds.
            lRequest.Timeout = 10000;
            try
            {
                // Get the response.
                WebResponse lResponse = lRequest.GetResponse();

                // Get the stream containing content returned by the server.
                Stream ldataStream = lResponse.GetResponseStream();
                // Open the stream using a StreamReader for easy access.
                StreamReader lreader = new StreamReader(ldataStream);
                // Read the content.
                lresponseFromServer = lreader.ReadToEnd();

                // Clean up the streams.
                lreader.Close();
                ldataStream.Close();
                lResponse.Close();
                return lresponseFromServer;
            }
            catch
            {
                //Timeout abfangen
                MessageBox.Show("Request timed out");
                return ""; //Programm verlassen

            }
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}
